<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Connexion extends CI_Controller {
    
	public function index()
	{
		$session_id = $this->session->userdata('id');
		$session_login = $this->session->userdata('nomEtudiant');

		if (!empty($session_id) && !empty($session_login)) {
			redirect('/accueil/', 'refresh');
		}

		$viewData = array();
		
		if (!empty($_POST)) {
			
			if (!empty($_POST['login']) && !empty($_POST['motdepasse'])) {

				// Connexion Base de Données
//				$dbconnect = $this->load->database(); // remplacé par auto connexion par l'autoload

				$this->load->Model('ConnexionModel');

				if ($this->ConnexionModel->login($_POST['login'], $_POST['motdepasse'])) {
					redirect('/accueil/', 'refresh');
				} else {
					$viewData['errorConnexion'] = true;
				}
			} else {
				$viewData['errorSaisieConnexion'] = true;
			}
		}
		

		 $this->template->set('title', 'Connexion');
		 $this->template->set('messageContents', '');
		 $this->template->load('layout','connexion', $viewData);
	}
        
    public function deconnecter()
	{     
		$user_data = $this->session->all_userdata();

		foreach ($user_data as $key => $value) {
			if ($key != 'session_id' && $key != 'ip_address' && $key != 'user_agent' && $key != 'last_activity') {
				$this->session->unset_userdata($key);
			}
		}

		redirect('/connexion/index/', 'refresh');
	}
        
}